package com.lahui.example

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.MotionEvent
import android.view.WindowManager
import android.view.animation.*
import android.widget.ImageView

class MainActivity : AppCompatActivity() {
    private val TAG = "LAHUI_EXAMPLE"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        createFloatView()
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun createFloatView() {
        //获取状态栏的高度
        val resourceId: Int = resources.getIdentifier("status_bar_height", "dimen", "android")
        val statusBarHeight: Int = resources.getDimensionPixelSize(resourceId)

        //创建悬浮窗的图片，这里当然也可以用自定义的View，这里之用了简单的图片
        var imageView = ImageView(this)
        imageView.setImageResource(R.mipmap.ic_launcher)

        var layoutParams = WindowManager.LayoutParams()
        var windowManager: WindowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager

        layoutParams.format = PixelFormat.RGBA_8888
        layoutParams.flags =
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        layoutParams.gravity = Gravity.TOP or Gravity.LEFT
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT

        //初始值是屏幕一半的地方，然后贴边
        layoutParams.x = 0
        //如果需要记录位置，可以用sp去做，先从sp中获取位置，赋值。
        layoutParams.y =
            resources.displayMetrics.heightPixels / 2 - statusBarHeight - imageView.measuredHeight / 2

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION
        } else
            layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

        windowManager.addView(imageView, layoutParams)
        imageView.isClickable = true;



        imageView.setOnTouchListener { v, event ->
            var downX = 0.0f
            var downY = 0.0f
            var moveX = 0.0f
            var moveY = 0.0f
            when (event?.action) {
                MotionEvent.ACTION_DOWN -> {
                    //记录手指按下的位置
                    downX = event.rawX - (imageView.width / 2)
                    downY = event.rawY - (imageView.height / 2) - statusBarHeight
                }
                MotionEvent.ACTION_MOVE -> {
                    moveX = event.rawX
                    moveY = event.rawY
                    //更新移动位置
                    layoutParams.x = (moveX - downX - (imageView.width / 2)).toInt()
                    layoutParams.y =
                        (moveY - downY - (imageView.height / 2) - statusBarHeight).toInt()
                    windowManager.updateViewLayout(imageView, layoutParams)
                }
                //贴边操作
                MotionEvent.ACTION_UP -> {
                    //中间点X
                    val centerX = resources.displayMetrics.widthPixels / 2
                    val upX = event.rawX
                    val upY = event.rawY
                    if (upX > centerX) { //贴右边
                        layoutParams.x = resources.displayMetrics.widthPixels
                    } else {//贴左边
                        layoutParams.x = 0
                    }
                    layoutParams.y =
                        upY.toInt() - imageView.height / 2 - statusBarHeight //y轴不变,但是要减去半个图片的高度和一个状态栏的高度

                    var animation: Animation = TranslateAnimation(
                        upX, layoutParams.x.toFloat(), upY, layoutParams.y.toFloat()
                    )

                    windowManager.updateViewLayout(imageView, layoutParams)
                }

            }
            false
        }

        imageView.setOnClickListener {
            //展示popupwindow
            showPopupWindow()
        }
    }

    private fun showPopupWindow() {

    }

}